package com.digitalchina.lxnsy.activity.old;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessRealTimePrice;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.TopSortVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.RealTimePriceSearchTypeHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/12/1 15:04
 * 修改备注：
 * ================================================
 */
public class RealTimePriceTabSearchActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.tv_search)
    EditText tvSearch;
    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private String keyWords = "";
    private int page = 1;
    private int limit = 20;
    private RecyclerArrayAdapter adapter;
    private List<TopSortVO> typeList = new ArrayList<>();

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_realtime_price_tab_search);
        setTitle("类型搜索");
    }

    @Override
    public void initMember() {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(Utils.setDividerDecoration(this, 1.0f, 10.0f, 10.0f));
        recyclerView.setAdapter(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new RealTimePriceSearchTypeHolder(parent);
            }
        });

    }

    @Override
    public void addListener() {

        recyclerView.setRefreshListener(this);
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore);
        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        tvSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // 隐藏软键盘
                    InputMethodManager imm = (InputMethodManager) textView
                            .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm.isActive()) {
                        imm.hideSoftInputFromWindow(textView.getApplicationWindowToken(), 0);
                    }
                    // 刷新列表数据
                    if (!keyWords.equals(tvSearch.getText().toString())) {
                        keyWords = tvSearch.getText().toString();
                        onRefresh();
                    }
                    return true;
                }
                return false;
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent();
                intent.putExtra("selectType", (Serializable) adapter.getItem(position));
                setResult(999, intent);
                finish();
            }
        });

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_PRODUCT_TYPE_BY_KEY_WORDS_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_PRODUCT_TYPE_BY_KEY_WORDS_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

        typeList = FastJsonUtil.getListBean(object.toString(), "body", "typeList", TopSortVO.class);
        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(typeList);

        if (typeList != null) {
            if (typeList.size() < limit) {
                adapter.stopMore();
            }
        }

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessRealTimePrice.getProductTypeByKeyWords(this, keyWords, page, limit, mHandler);
    }

    @Override
    public void onLoadMore() {
        if (typeList != null && typeList.size() == limit) {
            page++;
            BusinessRealTimePrice.getProductTypeByKeyWords(this, keyWords, page, limit, mHandler);
        }
    }
}
